<script setup lang="ts">
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { login } from '@/api/passport'

const form = reactive({
  username: 'user1',
  password: 'password'
})

const onSubmit = () => {
  console.log('submit!', form)
  login(form.username, form.password).then((response: any) => {
    console.log(response)
    let success = response.success
    let message = response.message
    if (success) {
      ElMessage({
        message: message,
        type: 'success',
        onClose: function () {
          location.href = import.meta.env.VITE_APP_BASE_API + response.data
        }
      })
    } else {
      ElMessage.error(message)
    }
  })
}
</script>

<template>
  <div id="login-view">
    <el-container>
      <el-header> </el-header>

      <el-main>
        <el-form :model="form" label-width="auto">
          <el-form-item label="用户名">
            <el-input v-model="form.username" name="username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              type="password"
              v-model="form.password"
              name="password"
              placeholder="请输入密码"
            />
          </el-form-item>
          <el-form-item label=" ">
            <el-button class="on-submit" @click="onSubmit">登录</el-button>
          </el-form-item>
        </el-form>
      </el-main>

      <el-footer> </el-footer>
    </el-container>
  </div>
</template>

<style scoped>
#login-view {
  max-width: 600px;
  margin: auto;

  .on-submit {
    width: 100%;
  }
}
</style>
